package com.xh.bussiness.permission.util;

import com.alibaba.fastjson.JSONObject;
import com.xh.core.properties.XhProperties;
import com.xh.core.util.CacheUtils;
import com.xh.core.util.MD5Utils;
import com.xh.core.util.RSAUtils;
import com.xh.core.util.StringUtils;
import lombok.Data;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/*
 * @Author daiwei
 * @Description
 * @Date 2019/2/13 14:25
 **/
@Component
@Data
public class UserAuthUtil {

    @Autowired
    private static XhProperties xhProperties;

    @Autowired
    private XhProperties _xhProperties;

    @PostConstruct
    public void init() {
        xhProperties=_xhProperties;
    }

    public static void main(String[] args) {
        String m = "18888888888";
        System.out.println(MD5Utils.string2MD5("ca2e1e650f72458d806cdda6546924d1" + "ef73781effc5774100f87fe2f437a435" + "xiaohe.com"));
    }
    /**
     * 加密密码
     * @param userId 用户ID
     * @param md5SourcePassword MD5加密的原始密码
     * @return
     */
    public static String encodePassword(String userId,String md5SourcePassword){
        //生成规则用户ID+MD5加密的原始密码+盐值
        return MD5Utils.string2MD5(userId+md5SourcePassword+xhProperties.getEncodeSalt());
    }

    /**
     * 生成Token
     * @param userId
     * @param username
     * @return
     */
    public static String generateToken(Integer os,Integer system,String userId,String username){
        //生成规则用户ID+用户名+盐值+10位随机字符串
        String token= MD5Utils.string2MD5(userId+username+xhProperties.getEncodeSalt()+ StringUtils.randomString(10));
        JSONObject json=new JSONObject();
        json.put("accessToken",token);
        json.put("userId",userId);
        json.put("username",username);
        CacheUtils.set("token_"+os+"_"+system+"_"+userId,json);
        return token;
    }

    /**
     * 检查密码正确性
     * @param userPassword
     * @param md5SourcePassword
     * @return
     */
    public static boolean checkPassword(Integer os,Integer system,String userId,String userPassword,String md5SourcePassword){
        if(StringUtils.isEmpty(md5SourcePassword)){
            return false;
        }
        String encodePassword=encodePassword(userId,md5SourcePassword);
        if(userPassword.equals(encodePassword)){
            return true;
        }
        return false;
    }

    /**
     * 检查Token正确性
     * @param userId
     * @param token
     * @return
     */
    public static boolean checkToken(Integer os,Integer system,String userId,String token){
        if(StringUtils.isEmpty(token)){
            return false;
        }
        JSONObject tokenJson=(JSONObject)CacheUtils.get("token_"+os+"_"+system+"_"+userId);
        if(tokenJson!=null&&token.equals(tokenJson.getString("accessToken"))){
            return true;
        }
        return false;
    }

    /**
     * 过期验证
     * @author 赵文博
     * @date 2021/4/7 15:33
     * @param publicKey
     * @return boolean
     **/
    public static boolean checkExpireDate(String publicKey){
        LocalDate now = LocalDate.now();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        try {

            String data = IOUtils.toString(UserAuthUtil.class.getClassLoader().getResourceAsStream("expireDate.txt"));
            if(StringUtils.isEmpty(data)){
                return false;
            }
            String expireDate = RSAUtils.decryptByPublicKey(publicKey,data);
            if("999".equals(expireDate)){
                return true;
            }
            LocalDate expireTime = LocalDate.parse(expireDate, df);
            return now.isBefore(expireTime);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
